package com.updata.up.utils;

import com.alibaba.fastjson.JSON;
import org.springframework.http.*;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

public class HttpSendUtil {
    /**
     * json方式请求 post请求
     *
     * @param url 请求地址
     * @param p   json格式数据或实体类
     * @return
     */
    public static <T> String postRequest(String url, T p) {
        HttpHeaders headers = new HttpHeaders();
        // 以json的方式提交
        headers.setContentType(MediaType.APPLICATION_JSON);
        return basePostRequest(url, p, headers);
    }

    /**
     * json方式请求 get
     *
     * @param url 请求地址
     * @param p   json格式数据
     * @return
     */
    public static String getRequest(String url, String p) {
        return baseGetRequest(url, p);
    }


    /**
     * 表单提交方式
     *
     * @param url    请求地址
     * @param params 数据
     * @return
     */
    public static String postRequests(String url, MultiValueMap<String, String> params) {
        HttpHeaders headers = new HttpHeaders();
        // 以表单的方式提交
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        return basePostRequest(url, params, headers);
    }

    public static <P> String basePostRequest(String url, P p, HttpHeaders headers) {
        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
        //连接到主机 超时时间 1分
        factory.setConnectTimeout(1000 * 60);
        //设置连接请求超时
        factory.setConnectionRequestTimeout(1000 * 60);
        //从主机读取数据 超时时间
        factory.setReadTimeout(120 * 1000);
        //创建RestTemplate对象
        RestTemplate client = new RestTemplate(factory);
        // 将请求头部和参数合成一个请求
        HttpEntity<P> requestEntity = new HttpEntity<>(p, headers);
        // 执行HTTP请求，将返回的结构使用String类格式化
        ResponseEntity<String> response = client.exchange(url, HttpMethod.POST, requestEntity, String.class);
        return response.getBody();
    }

    /**
     * get请求方法
     *
     * @param url 地址
     * @param p   json字符串
     * @return
     */
    public static String baseGetRequest(String url, String p) {
        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
        //连接到主机 超时时间 1分
        factory.setConnectTimeout(1000 * 60);
        //设置连接请求超时
        factory.setConnectionRequestTimeout(1000 * 60);
        //从主机读取数据 超时时间
        factory.setReadTimeout(120 * 1000);
        //创建RestTemplate对象
        RestTemplate client = new RestTemplate(factory);
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        Map<String, String> map = JSON.parseObject(p, Map.class);
        for (String key : map.keySet()) {
            String value = map.get(key);
            params.add(key, value);
        }
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
        URI uri = builder.queryParams(params).build().encode().toUri();
        ResponseEntity<String> response = client.getForEntity(uri, String.class);
        return response.getBody();
    }


    public static void main(String[] args) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
//        String url = "http://realip.cc";
//        String json = "{\"ip\":\"1.1.1.1\"}";
//        String send = getRequest(url, json);
//        System.out.println(send);
//        MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
//        map.add("request_time","1662532676192");
//        map.add("data","{\"project_name\":\"up-0\"}");
//        map.add("request_token","9b85c1e78bb72d51181dd37d6149f161");
//        String s =HttpSendUtil.postRequests("http://107.151.203.20:1314/project/java/restart_project", map);
//        System.out.println(s);
//

        MultiValueMap<String, String> maps = new LinkedMultiValueMap<String, String>();
        maps.add("request_time","1662532676192");
        maps.add("sfile","/www/wwwroot/up-0.1.jar");
        maps.add("dfile","/www/wwwroot/up-0.1.jar.1bak");
//        maps.add("rename","true");
        maps.add("request_token","9b85c1e78bb72d51181dd37d6149f161");
        String ss =HttpSendUtil.postRequests("http://107.151.203.20:1314/files?action=MvFile", maps);
        System.out.println(ss);
    }
}